{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Hidden Markov Models"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "author: Jacob Schreiber <br>\n",
    "contact: jmschreiber91@gmail.com"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Hidden Markov models (HMMs) are the flagship of the pomegranate package, in that most time is spent improving their implementation, and these improvements sometimes trickle down into the other algorithms. Lets delve into the features which pomegranate offers.\n",
    "\n",
    "Hidden Markov models are a form of structured prediction method which extend general mixture models to sequences of data, where position in the sequence is relevant. If each point in this sequence is completely independent of the other points, then HMMs are not the right tools and GMMs (or more complicated Bayesian networks) may be a better tool.\n",
    "\n",
    "The most common examples of HMMs come from bioinformatics and natural language processing. Since I am a bioinformatician, I will predominately use examples from bioinformatics.\n",
    "\n",
    "## GMMs vs HMMs in the CG rich region example\n",
    "\n",
    "Lets take the simplified example of CG island detection on a sequence of DNA. DNA is made up of the four canonical nucleotides, abbreviated 'A', 'C', 'G', and 'T'. Specific organizations of these nucleotides encode enough information to build you, a human being. One simple region in the genome is called the 'CG' island, where the nucleotides 'C' and 'G' are enriched. Lets compare the predictions of a GMM with the predictions of a HMM, to both understand conceptually the differences between the two, and to see how easy it is to use pomegranate."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Populating the interactive namespace from numpy and matplotlib\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/nfliu/miniconda2/envs/pom/lib/python2.7/site-packages/IPython/core/magics/pylab.py:161: UserWarning: pylab import has clobbered these variables: ['log', 'random']\n",
      "`%matplotlib` prevents importing * from pylab and numpy\n",
      "  \"\\n`%matplotlib` prevents importing * from pylab and numpy\"\n"
     ]
    }
   ],
   "source": [
    "from pomegranate import *\n",
    "import numpy as np\n",
    "%pylab inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "seq = list('CGACTACTGACTACTCGCCGACGCGACTGCCGTCTATACTGCGCATACGGC')\n",
    "\n",
    "d1 = DiscreteDistribution({'A': 0.25, 'C': 0.25, 'G': 0.25, 'T': 0.25})\n",
    "d2 = DiscreteDistribution({'A': 0.10, 'C': 0.40, 'G': 0.40, 'T': 0.10})\n",
    "\n",
    "s1 = State( d1, name='background' )\n",
    "s2 = State( d2, name='CG island' )\n",
    "\n",
    "gmm = GeneralMixtureModel( [d1, d2] )\n",
    "hmm = HiddenMarkovModel()\n",
    "hmm.add_states(s1, s2)\n",
    "hmm.add_transition( hmm.start, s1, 0.5 )\n",
    "hmm.add_transition( hmm.start, s2, 0.5 )\n",
    "hmm.add_transition( s1, s1, 0.5 )\n",
    "hmm.add_transition( s1, s2, 0.5 )\n",
    "hmm.add_transition( s2, s1, 0.5 )\n",
    "hmm.add_transition( s2, s2, 0.5 )\n",
    "hmm.bake()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sequence: CGACTACTGACTACTCGCCGACGCGACTGCCGTCTATACTGCGCATACGGC\n",
      "gmm pred: 110100101010010111110111101011110100001011110001111\n",
      "hmm pred: 001011010101101000001000010100001011110100001110000\n"
     ]
    }
   ],
   "source": [
    "gmm_predictions = gmm.predict( np.array(seq) )\n",
    "hmm_predictions = hmm.predict( seq )\n",
    "\n",
    "print \"sequence: {}\".format( ''.join( seq ) )\n",
    "print \"gmm pred: {}\".format( ''.join( map( str, gmm_predictions ) ) )\n",
    "print \"hmm pred: {}\".format( ''.join( map( str, hmm_predictions ) ) )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note: The HMM and GMM predictions may be the inverse of each other, because HMM states undergo a topological sort in order to properly handle silent states (more later), which can cause the order they were inserted into the model. \n",
    "\n",
    "Your first reaction may to say \"But Jacob, you just said that HMMs and GMMs are different. Why shold I make a HMM when making a GMM is so easy?\". \n",
    "\n",
    "My point in showing you this is that a dense HMM with equal probabilities between each state is ~equivalent~ to a GMM. However, this framework gives us great flexibility to add prior knowledge, whereas a GMM doesn't. If we look at the predictions, we see that it's bifurcating between \"background\" and \"CG island\" very quickly--in essence, calling every C or G a 'CG island'. This is not likely to be true. We know that CG islands have some As and Ts in them, and background sequence has Cs and Gs. We can change the transition probabilities to account for this, and prevent switching from occurring too rapidly."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "hmm = HiddenMarkovModel()\n",
    "hmm.add_states(s1, s2)\n",
    "hmm.add_transition( hmm.start, s1, 0.5 )\n",
    "hmm.add_transition( hmm.start, s2, 0.5 )\n",
    "hmm.add_transition( s1, s1, 0.9 )\n",
    "hmm.add_transition( s1, s2, 0.1 )\n",
    "hmm.add_transition( s2, s1, 0.1 )\n",
    "hmm.add_transition( s2, s2, 0.9 )\n",
    "hmm.bake()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sequence: CGACTACTGACTACTCGCCGACGCGACTGCCGTCTATACTGCGCATACGGC\n",
      "gmm pred: 110100101010010111110111101011110100001011110001111\n",
      "hmm pred: 111111111111111000000000000000011111111111111110000\n",
      "\n",
      "hmm state 0: CG island\n",
      "hmm state 1: background\n"
     ]
    }
   ],
   "source": [
    "hmm_predictions = hmm.predict( seq )\n",
    "\n",
    "print \"sequence: {}\".format( ''.join( seq ) )\n",
    "print \"gmm pred: {}\".format( ''.join( map( str, gmm_predictions ) ) )\n",
    "print \"hmm pred: {}\".format( ''.join( map( str, hmm_predictions ) ) )\n",
    "print\n",
    "print \"hmm state 0: {}\".format( hmm.states[0].name )\n",
    "print \"hmm state 1: {}\".format( hmm.states[1].name )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This seems far more reasonable. There is a single CG island surrounded by background sequence, and something at the end. If we knew that CG islands cannot occur at the end of sequences, we need only modify the underlying structure of the HMM in order to say that the sequence must end from the background state."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "hmm = HiddenMarkovModel()\n",
    "hmm.add_states(s1, s2)\n",
    "hmm.add_transition( hmm.start, s1, 0.5 )\n",
    "hmm.add_transition( hmm.start, s2, 0.5 )\n",
    "hmm.add_transition( s1, s1, 0.89 )\n",
    "hmm.add_transition( s1, s2, 0.10 )\n",
    "hmm.add_transition( s1, hmm.end, 0.01 )\n",
    "hmm.add_transition( s2, s1, 0.1 )\n",
    "hmm.add_transition( s2, s2, 0.9 )\n",
    "hmm.bake()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sequence: CGACTACTGACTACTCGCCGACGCGACTGCCGTCTATACTGCGCATACGGC\n",
      "gmm pred: 110100101010010111110111101011110100001011110001111\n",
      "hmm pred: 111111111111111000000000000000011111111111111111111\n",
      "\n",
      "hmm state 0: CG island\n",
      "hmm state 1: background\n"
     ]
    }
   ],
   "source": [
    "hmm_predictions = hmm.predict( seq )\n",
    "\n",
    "print \"sequence: {}\".format( ''.join( seq ) )\n",
    "print \"gmm pred: {}\".format( ''.join( map( str, gmm_predictions ) ) )\n",
    "print \"hmm pred: {}\".format( ''.join( map( str, hmm_predictions ) ) )\n",
    "print\n",
    "print \"hmm state 0: {}\".format( hmm.states[0].name )\n",
    "print \"hmm state 1: {}\".format( hmm.states[1].name )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Looks like we managed to get rid of that pesky end (again, the numbers may have flipped, look at the indices). Modifying transition probabilities and using non-dense graphical structures are two major ways in which HMMs account for data in a sequence not being independent and identically distributed (i.i.d.). In fact, in most applications, the graphical structure of a HMM is very sparse. \n",
    "\n",
    "If we want a more probabilistic view of what's going on, we can get the probability of each symbol in the sequence being in each of the states in the model easily. This is useful to get a soft estimate of classification, which allows us to include confidence as well as prediction. Values close to 50-50 get masked when you make hard classifications, but this uncertainty can be passed to future applications if you use soft assignments. Each row in the matrix is one symbol in the sequence, and the columns correspond to the two states identified above (CG island or background)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.4827054   0.5172946 ]\n",
      " [ 0.42204064  0.57795936]\n",
      " [ 0.28598775  0.71401225]\n",
      " [ 0.25756456  0.74243544]\n",
      " [ 0.16040038  0.83959962]\n",
      " [ 0.13871983  0.86128017]\n",
      " [ 0.17217854  0.82782146]\n",
      " [ 0.14750165  0.85249835]\n",
      " [ 0.18021186  0.81978814]\n",
      " [ 0.15446181  0.84553819]\n",
      " [ 0.18788044  0.81211956]\n",
      " [ 0.16252887  0.83747113]\n",
      " [ 0.19841088  0.80158912]\n",
      " [ 0.32919701  0.67080299]\n",
      " [ 0.38366073  0.61633927]\n",
      " [ 0.58044619  0.41955381]\n",
      " [ 0.69075524  0.30924476]\n",
      " [ 0.74653183  0.25346817]\n",
      " [ 0.76392808  0.23607192]\n",
      " [ 0.7479817   0.2520183 ]\n",
      " [ 0.69407484  0.30592516]\n",
      " [ 0.74761829  0.25238171]\n",
      " [ 0.76321595  0.23678405]\n",
      " [ 0.74538467  0.25461533]\n",
      " [ 0.68896078  0.31103922]\n",
      " [ 0.57760471  0.42239529]\n",
      " [ 0.58391467  0.41608533]\n",
      " [ 0.53953778  0.46046222]\n",
      " [ 0.6030831   0.3969169 ]\n",
      " [ 0.61516689  0.38483311]\n",
      " [ 0.57928847  0.42071153]\n",
      " [ 0.48505793  0.51494207]\n",
      " [ 0.30518744  0.69481256]\n",
      " [ 0.25379428  0.74620572]\n",
      " [ 0.12610747  0.87389253]\n",
      " [ 0.08105965  0.91894035]\n",
      " [ 0.07637934  0.92362066]\n",
      " [ 0.10767468  0.89232532]\n",
      " [ 0.20431225  0.79568775]\n",
      " [ 0.23273876  0.76726124]\n",
      " [ 0.35851961  0.64148039]\n",
      " [ 0.40985726  0.59014274]\n",
      " [ 0.40161837  0.59838163]\n",
      " [ 0.33141706  0.66858294]\n",
      " [ 0.17892403  0.82107597]\n",
      " [ 0.12850549  0.87149451]\n",
      " [ 0.13285026  0.86714974]\n",
      " [ 0.19603531  0.80396469]\n",
      " [ 0.19760431  0.80239569]\n",
      " [ 0.13801161  0.86198839]\n",
      " [ 0.          1.        ]]\n"
     ]
    }
   ],
   "source": [
    "print hmm.predict_proba( seq )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There is a corresponding `hmm.predict_log_proba` method present if you want to get the log values. These are the emission probability values calculated by the forward backward algorithm, and can also be retrieved by calling `hmm.forward_backward( seq )`, which returns both the emission and the transition probability tables.\n",
    "\n",
    "Lets take a look at these tables!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 15.78100555   2.89559314   0.           0.        ]\n",
      " [  2.41288774  28.91051356   0.           1.        ]\n",
      " [  0.4827054    0.5172946    0.           0.        ]\n",
      " [  0.           0.           0.           0.        ]]\n"
     ]
    }
   ],
   "source": [
    "trans, ems = hmm.forward_backward( seq )\n",
    "print trans"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is the transition table, which has the soft count of the number of transitions across an edge in the model given a single sequence. It is a square matrix of size equal to the number of states (including start and end state), with number of transitions from (row_id) to (column_id). This is exemplified by the 1.0 in the first row, indicating that there is one transition from background state to the end state, as that's the only way to reach the end state. However, the third (or fourth, depending on ordering) row is the transitions from the start state, and it only slightly favors the background state. These counts are not normalized to the length of the input sequence, but can easily be done so by dividing by row sums, column sums, or entire table sums, depending on your application.\n",
    "\n",
    "A possible reason not to normalize is to run several sequences through and add up their tables, because normalizing in the end and extracting some domain knowledge. It is extremely useful in practice. For example, we can see that there is an expectation of 2.8956 transitions from CG island to background, and 2.4 from background to CG island. This could be used to infer that there are ~2-3 edges, which makes sense if you consider that the start and end of the sequence seem like they might be part of the CG island states except for the strict transition probabilities used (look at the first few rows of the emission table above.)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We've been using the forward backward algorithm and maximum a posteriori for decoding thus far, however maximum a posteriori decoding has the side effect that it is possible that it predicts impossible sequences in some edge cases. An alternative is Viterbi decoding, which at each step takes the most likely path, instead of sum of all paths, to produce hard assignments."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sequence: CGACTACTGACTACTCGCCGACGCGACTGCCGTCTATACTGCGCATACGGC\n",
      "gmm pred: 110100101010010111110111101011110100001011110001111\n",
      "hmm pred: 111111111111111111111111111111111111111111111111111\n",
      "\n",
      "hmm state 0: CG island\n",
      "hmm state 1: background\n"
     ]
    }
   ],
   "source": [
    "hmm_predictions = hmm.predict( seq, algorithm='viterbi' )[1:-1]\n",
    "\n",
    "print \"sequence: {}\".format( ''.join( seq ) )\n",
    "print \"gmm pred: {}\".format( ''.join( map( str, gmm_predictions ) ) )\n",
    "print \"hmm pred: {}\".format( ''.join( map( str, hmm_predictions ) ) )\n",
    "print\n",
    "print \"hmm state 0: {}\".format( hmm.states[0].name )\n",
    "print \"hmm state 1: {}\".format( hmm.states[1].name )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We see here a case in which it does not do too well. The Viterbi path can be more conservative in its transitions due to the hard assignments it makes. In essence, if multiple possibile paths are possible at a given point, it takes the most likely path, even if the sum of all other paths is greater than the sum of that path. In problems with a lower signal to noise ratio, this can mask the signal. As a side note, we can use the following to get the maximum a posteriori and Viterbi paths:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "v_logp, v_seq = hmm.viterbi( seq )\n",
    "m_logp, m_seq = hmm.maximum_a_posteriori( seq )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The sequence predicted is a tuple of (state id, state object) for every state in the predicted path. The predict method simply takes the state ids from this path and returns those as an array. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Sequence Alignment"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Lets move on to a more complicated structure, that of a profile HMM. A profile HMM is used to align a sequence to a reference 'profile', where the reference profile can either be a single sequence, or an alignment of many sequences (such as a reference genome). In essence, this profile has a 'match' state for every position in the reference profile, and 'insert' state, and a 'delete' state. The insert state allows the external sequence to have an insertion into the sequence without throwing off the entire alignment, such as the following:\n",
    "\n",
    "`ACCG : Sequence` <br>\n",
    "`|| |` <br>\n",
    "`AC-G : Reference`\n",
    "\n",
    "or a deletion, which is the opposite:\n",
    "\n",
    "`A-G : Sequence` <br>\n",
    "`| |` <br>\n",
    "`ACG : Reference`\n",
    "\n",
    "The bars in the middle refer to a perfect match, whereas the lack of a bar means either a deletion/insertion, or a mismatch. A mismatch is where two positions are aligned together, but do not match. This models the biological phenomena of mutation, where one nucleotide can convert to another over time. It is usually more likely in biological sequences that this type of mutation occurs than that the nucleotide was deleted from the sequence (shifting all nucleotides over by one) and then another was inserted at the exact location (moving all nucleotides over again). Since we are using a probabilistic model, we get to define these probabilities through the use of distributions! If we want to model mismatches, we can just set our 'match' state to have an appropriate distribution with non-zero probabilities over mismatches. \n",
    "\n",
    "Lets now create a three nucleotide profile HMM, which models the sequence 'ACT'. We will fuzz this a little bit in the match states, pretending to have some prior information about what mutations occur at each position. If you don't have any information, setting a uniform, small, value over the other values is usually okay."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model = HiddenMarkovModel( \"Global Alignment\")\n",
    "\n",
    "# Define the distribution for insertions\n",
    "i_d = DiscreteDistribution( { 'A': 0.25, 'C': 0.25, 'G': 0.25, 'T': 0.25 } )\n",
    "\n",
    "# Create the insert states\n",
    "i0 = State( i_d, name=\"I0\" )\n",
    "i1 = State( i_d, name=\"I1\" )\n",
    "i2 = State( i_d, name=\"I2\" )\n",
    "i3 = State( i_d, name=\"I3\" )\n",
    "\n",
    "# Create the match states\n",
    "m1 = State( DiscreteDistribution({ \"A\": 0.95, 'C': 0.01, 'G': 0.01, 'T': 0.02 }) , name=\"M1\" )\n",
    "m2 = State( DiscreteDistribution({ \"A\": 0.003, 'C': 0.99, 'G': 0.003, 'T': 0.004 }) , name=\"M2\" )\n",
    "m3 = State( DiscreteDistribution({ \"A\": 0.01, 'C': 0.01, 'G': 0.01, 'T': 0.97 }) , name=\"M3\" )\n",
    "\n",
    "# Create the delete states\n",
    "d1 = State( None, name=\"D1\" )\n",
    "d2 = State( None, name=\"D2\" )\n",
    "d3 = State( None, name=\"D3\" )\n",
    "\n",
    "# Add all the states to the model\n",
    "model.add_states( [i0, i1, i2, i3, m1, m2, m3, d1, d2, d3 ] )\n",
    "\n",
    "# Create transitions from match states\n",
    "model.add_transition( model.start, m1, 0.9 )\n",
    "model.add_transition( model.start, i0, 0.1 )\n",
    "model.add_transition( m1, m2, 0.9 )\n",
    "model.add_transition( m1, i1, 0.05 )\n",
    "model.add_transition( m1, d2, 0.05 )\n",
    "model.add_transition( m2, m3, 0.9 )\n",
    "model.add_transition( m2, i2, 0.05 )\n",
    "model.add_transition( m2, d3, 0.05 )\n",
    "model.add_transition( m3, model.end, 0.9 )\n",
    "model.add_transition( m3, i3, 0.1 )\n",
    "\n",
    "# Create transitions from insert states\n",
    "model.add_transition( i0, i0, 0.70 )\n",
    "model.add_transition( i0, d1, 0.15 )\n",
    "model.add_transition( i0, m1, 0.15 )\n",
    "\n",
    "model.add_transition( i1, i1, 0.70 )\n",
    "model.add_transition( i1, d2, 0.15 )\n",
    "model.add_transition( i1, m2, 0.15 )\n",
    "\n",
    "model.add_transition( i2, i2, 0.70 )\n",
    "model.add_transition( i2, d3, 0.15 )\n",
    "model.add_transition( i2, m3, 0.15 )\n",
    "\n",
    "model.add_transition( i3, i3, 0.85 )\n",
    "model.add_transition( i3, model.end, 0.15 )\n",
    "\n",
    "# Create transitions from delete states\n",
    "model.add_transition( d1, d2, 0.15 )\n",
    "model.add_transition( d1, i1, 0.15 )\n",
    "model.add_transition( d1, m2, 0.70 ) \n",
    "\n",
    "model.add_transition( d2, d3, 0.15 )\n",
    "model.add_transition( d2, i2, 0.15 )\n",
    "model.add_transition( d2, m3, 0.70 )\n",
    "\n",
    "model.add_transition( d3, i3, 0.30 )\n",
    "model.add_transition( d3, model.end, 0.70 )\n",
    "\n",
    "# Call bake to finalize the structure of the model.\n",
    "model.bake()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now lets try to align some sequences to it and see what happens!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sequence: 'ACT'  -- Log Probability: -0.513244900357 -- Path: M1 M2 M3\n",
      "Sequence: 'GGC'  -- Log Probability: -11.0481012413 -- Path: I0 I0 D1 M2 D3\n",
      "Sequence: 'GAT'  -- Log Probability: -9.12551967402 -- Path: I0 M1 D2 M3\n",
      "Sequence: 'ACC'  -- Log Probability: -5.08795587886 -- Path: M1 M2 M3\n"
     ]
    }
   ],
   "source": [
    "for sequence in map( list, ('ACT', 'GGC', 'GAT', 'ACC') ):\n",
    "    logp, path = model.viterbi( sequence )\n",
    "    print \"Sequence: '{}'  -- Log Probability: {} -- Path: {}\".format(\n",
    "        ''.join( sequence ), logp, \" \".join( state.name for idx, state in path[1:-1] ) )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The first and last sequence are entirely matches, meaning that it thinks the most likely alignment between the profile ACT and ACT is A-A, C-C, and T-T, which makes sense, and the most likely alignment between ACT and ACC is A-A, C-C, and T-C, which includes a mismatch. Essentially, it's more likely that there's a T-C mismatch at the end then that there was a deletion of a T at the end of the sequence, and a separate insertion of a C.\n",
    "\n",
    "The two middle sequences don't match very well, as expected! G's are not very likely in this profile at all. It predicts that the two G's are inserts, and that the C matches the C in the profile, before hitting the delete state because it can't emit a T. The third sequence thinks that the G is an insert, as expected, and then aligns the A and T in the sequence to the A and T in the master sequence, missing the middle C in the profile.\n",
    "\n",
    "By using deletes, we can handle other sequences which are shorter than three characters. Lets look at some more sequences of different lengths."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sequence: 'A'  -- Log Probability: -5.40618101242 -- Path: M1 D2 D3\n",
      "Sequence: 'GA'  -- Log Probability: -10.8868199358 -- Path: I0 M1 D2 D3\n",
      "Sequence: 'AC'  -- Log Probability: -3.62447187905 -- Path: M1 M2 D3\n",
      "Sequence: 'AT'  -- Log Probability: -3.64488075068 -- Path: M1 D2 M3\n",
      "Sequence: 'ATCC'  -- Log Probability: -10.6743329646 -- Path: M1 D2 M3 I3 I3\n",
      "Sequence: 'ACGTG'  -- Log Probability: -10.3938248352 -- Path: M1 M2 I2 I2 I2 D3\n",
      "Sequence: 'ATTT'  -- Log Probability: -8.67126440175 -- Path: M1 I1 I1 D2 M3\n",
      "Sequence: 'TACCCTC'  -- Log Probability: -16.9034517961 -- Path: I0 I0 I0 I0 D1 M2 M3 I3\n",
      "Sequence: 'TGTCAACACT'  -- Log Probability: -16.4516996541 -- Path: I0 I0 I0 I0 I0 I0 I0 M1 M2 M3\n"
     ]
    }
   ],
   "source": [
    "for sequence in map( list, ('A', 'GA', 'AC', 'AT', 'ATCC', 'ACGTG', 'ATTT', 'TACCCTC', 'TGTCAACACT') ):\n",
    "    logp, path = model.viterbi( sequence )\n",
    "    print \"Sequence: '{}'  -- Log Probability: {} -- Path: {}\".format(\n",
    "        ''.join( sequence ), logp, \" \".join( state.name for idx, state in path[1:-1] ) )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Again, more of the same expected. You'll notice most of the use of insertion states are at I0, because most of the insertions are at the beginning of the sequence. It's more probable to simply stay in I0 at the beginning instead of go from I0 to D1 to I1, or going to another insert state along there. You'll see other insert states used when insertions occur in other places in the sequence, like 'ATTT' and 'ACGTG'.\n",
    "Now that we have the path, we need to convert it into an alignment, which is significantly more informative to look at."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sequence: A, Log Probability: -5.40618101242\n",
      "ACT\n",
      "A--\n",
      "\n",
      "Sequence: GA, Log Probability: -10.8868199358\n",
      "-ACT\n",
      "GA--\n",
      "\n",
      "Sequence: AC, Log Probability: -3.62447187905\n",
      "ACT\n",
      "AC-\n",
      "\n",
      "Sequence: AT, Log Probability: -3.64488075068\n",
      "ACT\n",
      "A-T\n",
      "\n",
      "Sequence: ATCC, Log Probability: -10.6743329646\n",
      "ACT--\n",
      "A-TCC\n",
      "\n",
      "Sequence: ACGTG, Log Probability: -10.3938248352\n",
      "AC---T\n",
      "ACGTG-\n",
      "\n",
      "Sequence: ATTT, Log Probability: -8.67126440175\n",
      "A--CT\n",
      "ATT-T\n",
      "\n",
      "Sequence: TACCCTC, Log Probability: -16.9034517961\n",
      "----ACT-\n",
      "TACC-CTC\n",
      "\n",
      "Sequence: TGTCAACACT, Log Probability: -16.4516996541\n",
      "-------ACT\n",
      "TGTCAACACT\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def path_to_alignment( x, y, path ):\n",
    "    \"\"\"\n",
    "    This function will take in two sequences, and the ML path which is their alignment,\n",
    "    and insert dashes appropriately to make them appear aligned. This consists only of\n",
    "    adding a dash to the model sequence for every insert in the path appropriately, and\n",
    "    a dash in the observed sequence for every delete in the path appropriately.\n",
    "    \"\"\"\n",
    "    \n",
    "    for i, (index, state) in enumerate( path[1:-1] ):\n",
    "        name = state.name\n",
    "        \n",
    "        if name.startswith( 'D' ):\n",
    "            y = y[:i] + '-' + y[i:]\n",
    "        elif name.startswith( 'I' ):\n",
    "            x = x[:i] + '-' + x[i:]\n",
    "\n",
    "    return x, y\n",
    "\n",
    "for sequence in map( list, ('A', 'GA', 'AC', 'AT', 'ATCC', 'ACGTG', 'ATTT', 'TACCCTC', 'TGTCAACACT') ):\n",
    "    logp, path = model.viterbi( sequence )\n",
    "    x, y = path_to_alignment( 'ACT', ''.join(sequence), path )\n",
    "    \n",
    "    print \"Sequence: {}, Log Probability: {}\".format( ''.join(sequence), logp )\n",
    "    print \"{}\\n{}\".format( x, y )\n",
    "    print"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In addition to getting this alignment, we can do some interesting things with this model! Lets score every sequence of length 5 of less and see what the distribution looks like."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAA1gAAAHLCAYAAADV8jlsAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAAPYQAAD2EBqD+naQAAIABJREFUeJzs3Xt41OWd///XnDKT02Qm5xAEBIGgWCKILYpSK9i1toLd\nq3Zda229WPu7qtT2266u1v2yrbuef+1i2W1rv5ZvRat2aXfbLdK6ioeqpS0ooEA4Qwgkk0zOh0nm\n+P1jMgMxHHKYmc9k5vm4Lq9OZj4zeU+5c3jlvu/3bYpEIhEBAAAAAMbNbHQBAAAAAJApCFgAAAAA\nkCAELAAAAABIEAIWAAAAACQIAQsAAAAAEoSABQAAAAAJQsACAAAAgAQhYAEA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z/Mb8AGrqamJxYsXM2PGDK655hrq6urCN9M0yc3NZerUqcybN4+dO3fy2muv8c4773DbbbdZXbr0\nM1OmTKGuro5ly5axd+9e3nnnHVauXMldd91ldWnSz+h7TXpKXl4eI0aM4D/+4z/45ptv2LhxIw8+\n+CA333wziYmJVpcnZzi3280NN9zAQw89xLZt21i/fj3PPvusutFLVBUWFhIbG8vChQspKyvj73//\nO48//jg//vGPu70Ow+y6etYA9ac//Yn77rsvYpppmhiGwfvvv09OTg719fUsWrSITz75hIyMDH72\ns59x7bXXWlSx9BdXXnkl99xzDzNmzAhP27x5M7/5zW/YuXMn6enp3HXXXfzzP/+zhVVKf3CsfU3f\na9JTqqur+fWvf82GDRswDIPrr7+eefPmdXv0LZHjaW9v55FHHmHdunUkJiYyd+5cZs2aZXVZ0s+U\nlJTw6KOPsmXLFuLj47nlllv4yU9+0u3XD/iAJSIiIiIiEi0DvougiIiIiIhItChgiYiIiIiIRIkC\nloiIiIiISJQoYImIiIiIiESJApaIiIiIiEiUKGCJiIiIiIhEiQKWiIiIiIhIlChgiYiIiIiIRIkC\nloiIiIiISJQoYImISFRdccUVPPnkk736np9//jkFBQURt3HjxnHJJZewaNEimpubT2v9CxYs4I47\n7ujRdezfv5+CggK++OILAGbNmsWCBQuA0OcbO3YslZWVADQ2NrJ69erTqkdERHqGw+oCREREosEw\nDFavXs3gwYMB6OjoYNeuXcyfP5+6ujpWrFhhcYXHl5OTw8cff0xycvJR8yZPnsxHH31EWloaAMuW\nLWP//v3cdNNNvV2miIicgAKWiIj0G6mpqaSnp4efZ2VlMXv2bJYvX05raysJCQkWVnd8hmFE1H44\nh8PxnfNERKRvURdBERHpdWvWrOGGG25g4sSJXHHFFTz11FMEg8Hw/H379vHjH/+YwsJCLr30Up57\n7jmmTZvGmjVrTvq9bDYbhmHgdDr5/PPPOeecc/jDH/7ABRdcEG4BOnDgAL/4xS+YOnUqhYWFzJkz\nh507d0asJxAIsGTJEoqKirjwwgtZvHgxPp8vPH/jxo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6c\nyJtvvkl7ezsHDx7kuuuuO611iojIqVEXQRER6TXp6ekMGjSITZs2RUz/4osvcDqd5ObmUlBQQHl5\necS1q0pKSmhpaemRmvLz8ykvL484V8rr9fKPf/yD0aNHh6cVFxdHvG7jxo243W5yc3N55ZVXGDRo\nECtXrmTOnDlccskl1NTUYBhGRIA71jpiY2PJzc097c8xc+ZM1q9fz1/+8heuuuqqPj1ioohIf6aA\nJSIiUbdnzx4+/PDDiFvX6Hhz5szhpZdeYtWqVezdu5e33nqL3//+99x8880kJCRw3XXXkZqayn33\n3ceOHTvYsmUL8+bN+84BILp0t9XryOWmT59OSkoK//7v/862bdvYsWMHv/jFL/B4PNx8883h5aqq\nqliwYAG7d+9m3bp1PPnkk8ydOxen00l2djYHDhzggw8+oLKyknfffZdHHnkEIGKkweOt42Tqjo+P\np6amJmLgj2uvvZampibeeOMNbrzxxm5tCxERiT51ERQRkah7++23efvttyOm5eTk8P7773PnnXcS\nExPD//zP//Doo4+SnZ3NXXfdxZw5c4BQl7tnnnmGxYsXc8stt5CcnMzdd9/Nr371q+N2pTtRAPuu\n5RISEnjhhRdYtmwZd955JwBFRUWsWrUqorvhlVdeicPh4J/+6Z+Ij4/ntttu4yc/+QkAs2bNoqys\njPvvvx+/38/w4cP5+c9/zn//93+zbds2pk6desJ1dNV2vJEEu8yYMYP33nuP6dOn8+6775KRkUFC\nQgJXXXUVX3zxBVOmTOnWthARkegzzFPt6C4iItID9u/fT3l5eURIqKmp4ZJLLuGll16iqKjIwur6\ntlmzZnHeeedx7733Wl2KiMiApRYsERHpU9rb27nrrru47777mDZtGs3Nzfznf/4nI0eOZNKkSVaX\n1ye9//77FBcXs3Xr1ojraomISO9TC5aIiPQ57777LitWrKCsrAyXy8WUKVOYN28egwcPtrq0PumW\nW26hvLycBx54gOuvv97qckREBjQFLBERERERkSjRKIIiIiIiIiJRooAlIiIiIiISJQpYIiIiIiIi\nUaKAJSIiIiIiEiUKWCIiIiIiIlGigCUiIiIiIhIlClgiIiIiIiJRooAlIiIiIiISJQpYIiIiIiIi\nUfL/AQmF7Tg6KczUAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x10db15250>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import itertools as it\n",
    "import seaborn as sns\n",
    "\n",
    "sequences = reduce( lambda x, y: x+y, [[ seq for seq in it.product( 'ACGT', repeat=i ) ] for i in xrange( 1,6 )] )\n",
    "scores = map( model.log_probability, sequences )\n",
    "\n",
    "plt.figure( figsize=(10,5) )\n",
    "sns.kdeplot( numpy.array( scores ), shade=True )\n",
    "plt.ylabel('Density')\n",
    "plt.xlabel('Log Probability')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Training Hidden Markov Models"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are two main algorithms for training hidden Markov models-- Baum Welch (structured version of Expectation Maximization), and Viterbi training. Since we don't start off with labels on the data, these are both unsupervised training algorithms. In order to assign labels, Baum Welch uses EM to assign soft labels (weights in this case) to each point belonging to each state, and then using weighted MLE estimates to update the distributions. Viterbi assigns hard labels to each observation using the Viterbi algorithm, and then updates the distributions based on these hard labels.\n",
    "\n",
    "pomegranate is extremely well featured when it comes to regularization methods for training, supporting tied emissions and edges, edge and emission inertia, freezing nodes or edges, edge pseudocounts, and multithreaded training. Lets look at some examples of the following:\n",
    "\n",
    "### Tied Emissions\n",
    "\n",
    "Sometimes we want to say that multiple states model the same phenomena, but are simply at different points in the graph because we are utilizing complicated edge structure. An example is in the example of the global alignment HMM we saw. All insert states represent the same phenomena, which is nature randomly inserting a nucleotide, and this probability should be the same regardless of position. However, we can't simply have a single insert state, or we'd be allowed to transition from any match state to any other match state.\n",
    "\n",
    "You can tie emissions together simply by passing the same distribution object to multiple states. That's it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "d = NormalDistribution( 5, 2 )\n",
    "\n",
    "s1 = State( d, name=\"Tied1\" )\n",
    "s2 = State( d, name=\"Tied2\" )\n",
    "\n",
    "s3 = State( NormalDistribution( 5, 2 ), name=\"NotTied1\" )\n",
    "s4 = State( NormalDistribution( 5, 2 ), name=\"NotTied2\" )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You have now indicated that these two states are tied, and when training, the weights of all points going to s2 will be added to the weights of all points going to s1 when updating d. As a side note, this is implemented in a computationally efficient manner such that d will only be updated once, not twice (but giving the same result). s3 and s4 are not tied together, because while they have the same distribution, it is not the same python object.\n",
    "\n",
    "### Tied Edges\n",
    "\n",
    "Edges can be tied together for the same reason. If you have a modular structure to your HMM, perhaps you believe this repeating structure doesn't (or shouldn't) have a position specific edge structure. You can do this simply by adding a group when you add transitions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "model = HiddenMarkovModel()\n",
    "model.add_states( [s1, s2] )\n",
    "model.add_transition( model.start, s1, 0.5, group='a' )\n",
    "model.add_transition( model.start, s2, 0.5, group='b' )\n",
    "model.add_transition( s1, s2, 0.5, group='a' )\n",
    "model.add_transition( s2, s1, 0.5, group='b' )\n",
    "model.bake()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The above model doesn't necessarily make sense, but it shows how simple it is to tie edges as well. You can go ahead and train normally from this point, without needing to change any code.\n",
    "\n",
    "### Inertia\n",
    "\n",
    "The next options are inertia on edges or on distributions. This simply means that you update your parameters as (previous_parameter * inertia) + (new_parameter * (1-inertia) ). It is a way to prevent your updates from overfitting immediately. You can specify this in the train function using either `edge_inertia` or `distribution_inertia`. These default to 0, with 1 being the maximum, meaning that you don't update based on new evidence, the same as freezing a distribution or the edges."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Training improvement: 1.42546236476\n",
      "Training improvement: 0.150770454545\n",
      "Training improvement: 0.0135693409091\n",
      "Training improvement: 0.00122124068182\n",
      "Training improvement: 0.000109911661365\n",
      "Training improvement: 9.89204952262e-06\n",
      "Training improvement: 8.90284457711e-07\n",
      "Training improvement: 8.01255986005e-08\n",
      "Training improvement: 7.21130533066e-09\n",
      "Training improvement: 6.49016840271e-10\n",
      "Total Training Improvement: 1.59114418288\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "1.5911441828758832"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.fit( [[5, 2, 3, 4], [5, 7, 2, 3, 5]], distribution_inertia=0.3, edge_inertia=0.25 )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Pseudocounts"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Another way of regularizing your model is to add pseudocounts to your edges (which have non-zero probabilities). When updating your edges in the future, you add this pseudocount to the count of transitions across that edge in the future. This gives a more Bayesian estimate of the edge probability, and is useful if you have a large model and don't expect to cross most of the edges with your training data. An example might be a complicated profile HMM, where you don't expect to see deletes or inserts at all in your training data, but don't want to change from the default values.\n",
    "\n",
    "In pomegranate, pseudocounts default to the initial probabilities, so that if you don't see data, the edge values simply aren't updated. You can define both edge specific pseudocounts when you define the transition. When you train, you must define `use_pseudocount=True`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Training improvement: 3.42346975202\n",
      "Training improvement: 0.217661114963\n",
      "Training improvement: 0.031578660226\n",
      "Training improvement: 0.00659919633505\n",
      "Training improvement: 0.00190080724972\n",
      "Total Training Improvement: 3.6812095308\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "3.6812095307959467"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1 = State( NormalDistribution( 3, 1 ), name=\"s1\" )\n",
    "s2 = State( NormalDistribution( 6, 2 ), name=\"s2\" )\n",
    "\n",
    "model = HiddenMarkovModel()\n",
    "model.add_states( [s1, s2] )\n",
    "model.add_transition( model.start, s1, 0.5, pseudocount=4.2 )\n",
    "model.add_transition( model.start, s2, 0.5, pseudocount=1.3 )\n",
    "model.add_transition( s1, s2, 0.5, pseudocount=5.2 )\n",
    "model.add_transition( s2, s1, 0.5, pseudocount=0.9 )\n",
    "model.bake()\n",
    "model.fit( [[5, 2, 3, 4], [5, 7, 2, 3, 5]], max_iterations=5, use_pseudocount=True )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The other way is to put a blanket pseudocount on all edges."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Training improvement: 3.32270811471\n",
      "Training improvement: 0.160909798897\n",
      "Training improvement: 0.0329521413094\n",
      "Training improvement: 0.00953914051447\n",
      "Training improvement: 0.00315874050675\n",
      "Total Training Improvement: 3.52926793594\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "3.5292679359352164"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1 = State( NormalDistribution( 3, 1 ), name=\"s1\" )\n",
    "s2 = State( NormalDistribution( 6, 2 ), name=\"s2\" )\n",
    "\n",
    "model = HiddenMarkovModel()\n",
    "model.add_states( [s1, s2] )\n",
    "model.add_transition( model.start, s1, 0.5 )\n",
    "model.add_transition( model.start, s2, 0.5 )\n",
    "model.add_transition( s1, s2, 0.5 )\n",
    "model.add_transition( s2, s1, 0.5 )\n",
    "model.bake()\n",
    "model.fit( [[5, 2, 3, 4], [5, 7, 2, 3, 5]], max_iterations=5, transition_pseudocount=20, use_pseudocount=True )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can see that there isn't as much of an improvement. This is part of regularization, though. We sacrifice fitting the data exactly in order for our model to generalize better to future data. The majority of the training improvement is likely coming from the emissions better fitting the data, though."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Multithreaded Training"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Since pomegranate is implemented in cython, the majority of functions are written with the GIL released. A benefit of doing this is that we can use multithreading in order to make some computationally intensive tasks take less time. However, a downside is that python doesn't play nicely with multithreading, and so there are some cases where training using multithreading can make your model training take significantly longer. I investigate this in an early multithreading pull request <a href=\"https://github.com/jmschrei/pomegranate/pull/30\">here</a>. Things have improved since then, but the gist is that if you have a small model (less than 15 states), it may be detrimental, but the larger your model is, the more it scales towards getting a speed improvement exactly the number of threads you use. You can specify multithreading using the `n_jobs` keyword. All structures in pomegranate are thread safe, so you don't need to worry about race conditions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Training improvement: 6.91636682627\n",
      "Training improvement: 0.807997703056\n",
      "Training improvement: 0.225451019863\n",
      "Training improvement: 0.0142426402922\n",
      "Training improvement: 0.000547351061158\n",
      "Total Training Improvement: 7.96460554054\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "7.964605540540248"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1 = State( NormalDistribution( 3, 1 ), name=\"s1\" )\n",
    "s2 = State( NormalDistribution( 6, 2 ), name=\"s2\" )\n",
    "\n",
    "model = HiddenMarkovModel()\n",
    "model.add_states( [s1, s2] )\n",
    "model.add_transition( model.start, s1, 0.5 )\n",
    "model.add_transition( model.start, s2, 0.5 )\n",
    "model.add_transition( s1, s2, 0.5 )\n",
    "model.add_transition( s2, s1, 0.5 )\n",
    "model.bake()\n",
    "model.fit( [[5, 2, 3, 4, 7, 3, 6, 3, 5, 2, 4], [5, 7, 2, 3, 5, 1, 3, 5, 6, 2]], max_iterations=5 )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Training improvement: 6.91636682627\n",
      "Training improvement: 0.807997703056\n",
      "Training improvement: 0.225451019863\n",
      "Training improvement: 0.0142426402922\n",
      "Training improvement: 0.000547351061158\n",
      "Total Training Improvement: 7.96460554054\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "7.964605540540248"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1 = State( NormalDistribution( 3, 1 ), name=\"s1\" )\n",
    "s2 = State( NormalDistribution( 6, 2 ), name=\"s2\" )\n",
    "\n",
    "model = HiddenMarkovModel()\n",
    "model.add_states( [s1, s2] )\n",
    "model.add_transition( model.start, s1, 0.5 )\n",
    "model.add_transition( model.start, s2, 0.5 )\n",
    "model.add_transition( s1, s2, 0.5 )\n",
    "model.add_transition( s2, s1, 0.5 )\n",
    "model.bake()\n",
    "model.fit( [[5, 2, 3, 4, 7, 3, 6, 3, 5, 2, 4], [5, 7, 2, 3, 5, 1, 3, 5, 6, 2]], max_iterations=5, n_jobs=4 )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Serialization"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "General Mixture Models support serialization to JSONs using `to_json()` and `from_json( json )`. This is useful is you want to train a GMM on large amounts of data, taking a significant amount of time, and then use this model in the future without having to repeat this computationally intensive step (sounds familiar by now). Lets look at the original CG island model, since it's significantly smaller."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "seq = list('CGACTACTGACTACTCGCCGACGCGACTGCCGTCTATACTGCGCATACGGC')\n",
    "\n",
    "d1 = DiscreteDistribution({'A': 0.25, 'C': 0.25, 'G': 0.25, 'T': 0.25})\n",
    "d2 = DiscreteDistribution({'A': 0.10, 'C': 0.40, 'G': 0.40, 'T': 0.10})\n",
    "\n",
    "s1 = State( d1, name='background' )\n",
    "s2 = State( d2, name='CG island' )\n",
    "\n",
    "hmm = HiddenMarkovModel()\n",
    "hmm.add_states(s1, s2)\n",
    "hmm.add_transition( hmm.start, s1, 0.5 )\n",
    "hmm.add_transition( hmm.start, s2, 0.5 )\n",
    "hmm.add_transition( s1, s1, 0.5 )\n",
    "hmm.add_transition( s1, s2, 0.5 )\n",
    "hmm.add_transition( s2, s1, 0.5 )\n",
    "hmm.add_transition( s2, s2, 0.5 )\n",
    "hmm.bake()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "    \"end\" : {\n",
      "        \"distribution\" : null,\n",
      "        \"class\" : \"State\",\n",
      "        \"weight\" : 1.0,\n",
      "        \"name\" : \"None-end\"\n",
      "    },\n",
      "    \"name\" : \"None\",\n",
      "    \"distribution ties\" : [],\n",
      "    \"silent_index\" : 2,\n",
      "    \"end_index\" : 3,\n",
      "    \"states\" : [\n",
      "        {\n",
      "            \"distribution\" : {\n",
      "                \"frozen\" : false,\n",
      "                \"class\" : \"Distribution\",\n",
      "                \"parameters\" : [\n",
      "                    {\n",
      "                        \"A\" : 0.1,\n",
      "                        \"C\" : 0.4,\n",
      "                        \"T\" : 0.1,\n",
      "                        \"G\" : 0.4\n",
      "                    }\n",
      "                ],\n",
      "                \"name\" : \"DiscreteDistribution\"\n",
      "            },\n",
      "            \"class\" : \"State\",\n",
      "            \"weight\" : 1.0,\n",
      "            \"name\" : \"CG island\"\n",
      "        },\n",
      "        {\n",
      "            \"distribution\" : {\n",
      "                \"frozen\" : false,\n",
      "                \"class\" : \"Distribution\",\n",
      "                \"parameters\" : [\n",
      "                    {\n",
      "                        \"A\" : 0.25,\n",
      "                        \"C\" : 0.25,\n",
      "                        \"T\" : 0.25,\n",
      "                        \"G\" : 0.25\n",
      "                    }\n",
      "                ],\n",
      "                \"name\" : \"DiscreteDistribution\"\n",
      "            },\n",
      "            \"class\" : \"State\",\n",
      "            \"weight\" : 1.0,\n",
      "            \"name\" : \"background\"\n",
      "        },\n",
      "        {\n",
      "            \"distribution\" : null,\n",
      "            \"class\" : \"State\",\n",
      "            \"weight\" : 1.0,\n",
      "            \"name\" : \"None-start\"\n",
      "        },\n",
      "        {\n",
      "            \"distribution\" : null,\n",
      "            \"class\" : \"State\",\n",
      "            \"weight\" : 1.0,\n",
      "            \"name\" : \"None-end\"\n",
      "        }\n",
      "    ],\n",
      "    \"start\" : {\n",
      "        \"distribution\" : null,\n",
      "        \"class\" : \"State\",\n",
      "        \"weight\" : 1.0,\n",
      "        \"name\" : \"None-start\"\n",
      "    },\n",
      "    \"edges\" : [\n",
      "        [\n",
      "            0,\n",
      "            1,\n",
      "            0.5,\n",
      "            0.5,\n",
      "            null\n",
      "        ],\n",
      "        [\n",
      "            0,\n",
      "            0,\n",
      "            0.5,\n",
      "            0.5,\n",
      "            null\n",
      "        ],\n",
      "        [\n",
      "            2,\n",
      "            1,\n",
      "            0.5,\n",
      "            0.5,\n",
      "            null\n",
      "        ],\n",
      "        [\n",
      "            2,\n",
      "            0,\n",
      "            0.5,\n",
      "            0.5,\n",
      "            null\n",
      "        ],\n",
      "        [\n",
      "            1,\n",
      "            1,\n",
      "            0.5,\n",
      "            0.5,\n",
      "            null\n",
      "        ],\n",
      "        [\n",
      "            1,\n",
      "            0,\n",
      "            0.5,\n",
      "            0.5,\n",
      "            null\n",
      "        ]\n",
      "    ],\n",
      "    \"class\" : \"HiddenMarkovModel\",\n",
      "    \"start_index\" : 2\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "print hmm.to_json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-69.7012190974\n"
     ]
    }
   ],
   "source": [
    "seq = list('CGACTACTGACTACTCGCCGACGCGACTGCCGTCTATACTGCGCATACGGC')\n",
    "print hmm.log_probability( seq )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-69.7012190974\n"
     ]
    }
   ],
   "source": [
    "hmm_2 = HiddenMarkovModel.from_json( hmm.to_json() )\n",
    "print hmm_2.log_probability( seq )"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
